﻿define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Confirm_payment, $stateParams,$ionicModal, GoPicture, GoDialog, GoPage,Auth) {
		
		$scope.store_id = $stateParams.id;
		function initialization(){
			$scope.input=new Object();
			$scope.input.password = "";
			$scope.input.photo = new Array();
			$scope.input.photo[0] = new Object();//只上传一张
			$scope.input.bankObj = new Object();
			$scope.imgUrl = "";//凭证图片
		}
		
		$scope.$on('$ionicView.afterEnter', function(){
			var Infos = Auth.get_loginInfo();
			$scope.issue_type = Infos.issue_type;//1买家    2卖家
			$scope.init_data();
		},false)
		
//		$scope.$on('$ionicView.beforeLeave', function() {
//			$scope.closeModal_confirm();
//		}, false);
		
		//初始化
		$scope.init_data = function() {
			initialization();
			var type = 0;
			if($scope.issue_type==1){
				type = 1;
			}
			Confirm_payment.agent_credit($scope.store_id, type, function(data) {
				if(!data) return;
				$scope.data = data;
				if(!data.confirm_amount){
					data.confirm_amount = 0;
				}
				$scope.debt_total = parseFloat(data.debt_total - data.confirm_amount);//欠款总数-待确认的金额
				$scope.payName = "预存款支付";//默认预存款支付
				$scope.input.payType = "2";
//				if($scope.debt_total.indexOf(".")==-1){//在后面加2个0
//					$scope.debt_total +=".00"; 
//				}
				$scope.input.money = $scope.debt_total;//$scope.debt_total后面用
			});
		}
		
		//清空输入
		$scope.clearinput=function(){
			$scope.input.money="";
		}
		
		//监听金额的输入
		$scope.$watch('input.money', function(newValue, oldValue) {
			if(!newValue) return;
			newValue = parseFloat(newValue);
			if(newValue>$scope.debt_total){
				GoDialog.toast("还款金额不能大于欠款金额");
				$scope.input.money = $scope.debt_total;
			}
		})
		
		/******************选择支付方式**************/
		$ionicModal.fromTemplateUrl("templates/modal/select_payType.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal_payType = modal;
		});
		//打开
		$scope.openModal_payType = function() {
			$scope.modal_payType.show();
		}
		//隐藏
		$scope.closeModal_payType = function() {
			$scope.modal_payType.hide();
		}
		//确认支付方式
		$scope.selectPayType = function(index) {
			var data = $scope.data.store_payment[index];
			$scope.payName = data.pay_name;
			$scope.input.payType = data.pay_app_id;
			$scope.closeModal_payType();
		}
		/******************选择支付方式**************/
		
		/******************选择账号**************/
		$ionicModal.fromTemplateUrl("templates/modal/select_storeBank.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal_bank = modal;
		});	
		$scope.openModal_bank = function() {
			$scope.modal_bank.show();
		}
		$scope.closeModal_bank = function(index) {
			$scope.modal_bank.hide();
		}
		//确认账号
		$scope.selectBank = function(index) {
			var data = $scope.data.store_bank[index];
			$scope.input.bankObj.bank_id = data.id;
			$scope.input.bankObj.receiver_name = data.name;
			$scope.input.bankObj.bank_name = data.bank;
			$scope.input.bankObj.bank_account = data.bank_no;
			$scope.closeModal_bank();
		}
		/******************选择账号**************/
		
		//图片上传，只能上传一张
		$scope.addImg = function() {
			GoPicture.upload(function(res) {
                if(res.success) {
                    $scope.input.photo[0].image = res.result.image_id;
                    $scope.imgUrl = res.result.image_src;
                }else{
                	GoDialog.alert("提示",res.msg);
                }
        	});
		};
		
		/******************确认支付密码**************/
		$ionicModal.fromTemplateUrl("templates/modal/confirm_pay.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal_confirm = modal;
		});
		//预存款支付
		$scope.openModal_confirm = function() {
			checkInput();
			if(!$scope.isLegal) return;
			//每次打开modal，必须初始化以下2个变量
			$scope.input.password = "";
			$scope.showPotArr = new Array();
			$scope.modal_confirm.show();//满足输入条件，才弹窗
			$scope.getInputFocus();
		}
		$scope.closeModal_confirm = function(index) {
			$scope.modal_confirm.hide();
		}
		//获取焦点
		$scope.getInputFocus = function($event){
			if($event){
				$event.stopPropagation();//阻止事件冒泡
			}
			document.getElementById("inputPass").focus();
		}
		
		//输入密码监听
		$scope.$watch('input.password', function(newValue, oldValue) {
			$scope.showPotArr = new Array();//控制密码点的显示
			if(!newValue){
				return
			}
			if(newValue.length>6) {//6为密码
				newValue = newValue.substring(0, 6);
			}
			$scope.input.password = newValue;
			for (var i=0;i<$scope.input.password.length;i++) {
				$scope.showPotArr.push(i+1);
			}
			if($scope.input.password.length==6){//密码够6位，自动提交
//				$scope.closeModal_confirm();//关闭弹窗不能放在这里，当支付不成功时，会导致整个页面不能再获得焦点。支付成功会跳转到其他页面，所以不会有这个问题。必须放在submitForm()里面
				document.getElementById("inputPass").blur();//防止继续输入
				submitForm();
			}
		})
		/******************确认支付密码**************/
		
		//检查输入
		function checkInput(){
			$scope.isLegal = true;
//			if(!$scope.input.payType) {
//				GoDialog.toast("请选择还款方式");
//				$scope.isLegal = false;
//				return;
//			}
			if(!$scope.input.money) {
				GoDialog.toast("请输入还款金额");
				$scope.isLegal = false;
				return
			}
			if($scope.input.payType == "2"){//预存款还款。需要：（1）输入支付密码
				if(parseFloat($scope.input.money) > parseFloat($scope.data.advance)) {
					GoDialog.toast("余额不足");
					$scope.isLegal = false;
					return;
				}
			}
			if($scope.input.payType == "1") {//线下还款。需要：（1）上传凭证（2）银行账号
				if(!$scope.input.bankObj.bank_name){//这里简单的判断bank_name
					GoDialog.toast("请选择对方账号");
					$scope.isLegal = false;
					return;
				}
				if(!$scope.input.photo[0].image) {
					GoDialog.toast("请上传还款凭证");
					$scope.isLegal = false;
					return;
				}
			}
		}
		
		//线下支付--提交
		$scope.offlinePay = function(){
			checkInput();
			if(!$scope.isLegal) return;
			submitForm();
		}
		
		//提交。//userType 默认0：供应商，1：好友店铺
		function submitForm(){
			var type = 0;
			if($scope.issue_type==1){
				type = 1;
			}
			Confirm_payment.credit_repay($scope.store_id, 
				$scope.input.money, 
				$scope.input.payType,
				$scope.input.photo,  
				$scope.input.password,
				$scope.input.bankObj,
				type,
				function(data) {
				if(data.success) {
					$scope.closeModal_confirm();
					GoDialog.toast(data.msg);
					initialization();
					GoPage.go_states("credit_repayment",{"id":$scope.store_id});
				} else{
					GoDialog.alert("提示",data.msg,function(){
						$scope.closeModal_confirm();
					});
				}
			})
		}
		
		
	}

	controller.$inject = ['$scope', 'Confirm_payment', '$stateParams','$ionicModal','GoPicture','GoDialog','GoPage','Auth'];

	return app.registerController("confirm_paymentctrl", controller);
});